<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css">
</head>

<body>
    <div style="margin: 20px">
        <span class="layui-breadcrumb">
            <a href="/home">首页</a>
            <a><cite>商品分类列表</cite></a>
        </span>
        <div id="add" style="margin-top: 10px">
            <a href="/cateAdd" class="layui-btn">增加分类</a>
        </div>
        <table class="layui-table">
            <colgroup>
                <col width="150">
                <col width="200">
                <col>
            </colgroup>
            <thead>
                <tr>
                    <th>分类名称</th>
                    <th>分类图标</th>
                    <th>分类描述</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <% for(var i=0;i<cates.length;i++) { %>
                <tr>
                    <td><%= cates[i].cateName %></td>
                    <td>
                        <img width="50px"
                            src="<%= cates[i].cateIcon?cates[i].cateIcon:'http://secretkeycrm.digifilm.com.cn/upload/20180530/e676f667c4cdfc7e074898adab2622f2.jpg' %>"
                            alt="">
                    </td>
                    <td><%= cates[i].cateDesc? cates[i].cateDesc:'暂无描述' %></td>
                    <td>
                        <a href="/cateEdit?id=<%= cates[i]._id %>" class="layui-btn layui-btn-xs">编辑</a>
                        <button data-id="<%= cates[i]._id %>" class="layui-btn layui-btn-xs layui-btn-danger del">删除</button>
                    </td>
                </tr>
                <% } %>
            </tbody>
        </table>
        <div id="page"></div>
    </div>
</body>
<script src="/static/lib/layui/layui.js"></script>
<script>
    layui.use(['element', 'laypage','jquery','layer'], function () {
        var element = layui.element;
        var laypage = layui.laypage;
        var $ = layui.jquery;
        var layer = layui.layer;
        $('.del').click(function(){
            const _id = this.dataset.id
            layer.confirm('你确定要删除吗?',{
                icon:3,
                title:'删除'
            },function(){
                $.ajax(
                    '/cateDel',
                    {
                        type:'POST',
                        data:{_id},
                        success:function(res){
                            const {code,msg} = res 
                            if(code===0){
                                layer.msg('删除成功',{time:1000},
                                function(){
                                    location.reload()
                                }
                            )
                            }else{
                                layer.msg(msg,{time:1000})
                            }
                        }
                    }
                )
            })
        })
        laypage.render({
            elem: 'page',
            curr: <%= current %>,
            count: <%= count %>,
            limit: 4,
            jump: function (obj, first) {
                //obj包含了当前分页的所有参数，比如：
                //console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                //console.log(obj.limit); //得到每页显示的条数

                //首次不执行
                if (!first) {
                    //do something
                    location.href = '/cateList?current='+obj.curr+'&limit='+obj.limit;
                }
            }
        });
    });
</script>
</script>

</html>